
/**
* @Title: SearchServciceImpl.java
* @Package com.leetone.search.service.impl
* @Description: 【】
* @date 2018年4月5日 上午8:37:07
* @version V1.0
* @Company: 上海
* @author:[谭磊][leetone123@163.com]
*/

package com.leetone.search.service.impl;

import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.leetone.search.dao.ItemSearchDao;
import com.leetone.search.pojo.SearchResult;
import com.leetone.search.service.SearchService;

/**
* @ClassName: SearchServciceImpl
* @Description: 【】
* @date 2018年4月5日 上午8:37:07
* @Company: 上海
* @author:[谭磊][leetone123@163.com]
*/
@Service("searchService")
public class SearchServciceImpl implements SearchService {
	@Autowired
	private ItemSearchDao itemSearchDao;
	@Override
	public SearchResult search(String queryString, int page, int rows) throws Exception{
		SolrQuery solrQuery =new SolrQuery();
		//创建查询条件
		solrQuery.setQuery(queryString);
		//设置分页
		solrQuery.setStart((page-1)*rows);
		solrQuery.setRows(rows);
		//设置默认搜索域
		solrQuery.set("df", "item_keywords");
		//设置高亮显示
		solrQuery.setHighlight(true);
		//设置高亮域
		solrQuery.addHighlightField("item_title");
		//设置高亮前缀【包括样式】
		solrQuery.setHighlightSimplePre("<em style=\"color:red;\" >");
		//设置高亮后缀
		solrQuery.setHighlightSimplePost("</em>");
		//执行查询
		SearchResult searchResult = itemSearchDao.searchItem(solrQuery);
		//计算总页数
		long count =searchResult.getRecordCount();
		long pageCount = count /rows;
		if(count % rows > 0)
		{
			pageCount++;
		}
		searchResult.setPageCount(pageCount);
		searchResult.setCurPage((long) page);
		return searchResult;
	}

}
